<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="12">
            <a-form-model-item label="学年" prop="user">
              <a-select v-model="searchParams.schoolYearId" placeholder="请选择考试学年"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in schoolYearDataList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <a-form-model-item label="学期" prop="user">
              <a-select v-model="searchParams.semesterId" placeholder="请选择考试学期"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in semesterDataList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :md="6" :sm="8">
              <a-form-model-item label="年级" prop="user">
                <a-select v-model="searchParams.gradeId" placeholder="请选择考试年级"
                          :getPopupContainer="(target) => target.parentNode">
                  <a-select-option v-for="item in gradeDataList" :key="item.value">{{ item.label }}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-model-item label="启用状态">
                <j-dict-select-tag type="list" v-model="searchParams.izOpen"
                                   dictCode="iz_open" placeholder="选择是否启用状态" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-model-item label="忽略">
                <j-checkbox
                  v-model="queryParam.ignoreProperty"
                  :options="queryParamSelect.ignorePropertyList"
                />
              </a-form-model-item>
            </a-col>
          </template>

          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="whereSerchAchExamination" icon="search">查询</a-button>
              <a-button type="primary" @click="whereSerchAchExaminationInit" icon="reload"
                        style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('ach_examination')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
                @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-button style="background-color: rgb(233, 175, 0);border: none;" @click="copyExamAllThings()"
                type="primary" icon="copy">复制考试项目
      </a-button>
      <!-- 高级查询区域 -->
      <!--      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
      <!-- 新加 按条件查询 -->

      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
      <template>
        <span style="float:right;">
          <a @click="searchQuery()"><a-icon type="sync" />刷新</a>
          <a-divider type="vertical" />
          <a-button v-print="'#datatable'" ghost type="primary">打印</a-button>
        </span>
      </template>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        filterMultiple="filterMultiple"
        :columns="columns"
        :dataSource="achExaminationList"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange,type:type}"
        class="j-table-force-nowrap"
        @change="handleTableChange"
        :customRow="clickThenCheck">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <a-modal @ok="submitCopy" :visible="copyExamVisible" @cancel="copyExamVisible = false">
      <a-form :model="newExamInfo">
        <a-row>
          <a-col :span="24">
            <a-form-item label="考试编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationCode">
              <a-input v-model="newExamInfo.examinationCode" placeholder="请输入考试编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="考试名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationName">
              <a-input v-model="newExamInfo.examinationName" placeholder="请输入考试名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="考试类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationTtype">
              <j-dict-select-tag type="list" v-model="newExamInfo.examinationTtype"
                                 dictCode="examination_ttype" placeholder="请选择考试类型" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="年级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gradeId">
              <a-select v-model="newExamInfo.gradeId" placeholder="请选择考试年级"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in gradeDataList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="学年" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="schoolYearId">
              <a-select v-model="newExamInfo.schoolYearId" placeholder="请选择考试学年"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in schoolYearDataList" :key="item.value">{{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="学期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="semesterId">
              <a-select v-model="newExamInfo.semesterId" placeholder="请选择考试学期"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in semesterDataList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="izOpen">
              <j-dict-select-tag type="list" v-model="newExamInfo.izOpen"
                                 dictCode="iz_open" placeholder="请选择是否启用" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="所属部门编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sysOrgCode">
              <j-dict-select-tag v-model="newExamInfo.sysOrgCode"
                                 dictCode="sys_depart,depart_name,org_code"></j-dict-select-tag>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="考试开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sysOrgCode">
              <j-date v-model="examStartTime"
                      dictCode="sys_depart,depart_name,org_code"></j-date>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-modal>

    <ach-examination-modal ref="modalForm" @ok="modalFormOk"></ach-examination-modal>

    <a-tabs defaultActiveKey="1">
      <a-tab-pane tab="各科试卷" key="1">
        <ach-examination-paper-list-tables ref="AchExaminationPaperListTables"></ach-examination-paper-list-tables>
      </a-tab-pane>
      <a-tab-pane tab="计分规则" key="2" forceRender>
        <ach-achieve-standard-item-list-table
          ref="AchAchieveStandardItemListTable"></ach-achieve-standard-item-list-table>
      </a-tab-pane>
      <a-tab-pane tab="评分标准" key="3" forceRender>
        <ach-achieve-standard-item-stand-list-table ref="AchAchieveStandardItemStandListTable">
        </ach-achieve-standard-item-stand-list-table>
      </a-tab-pane>
    </a-tabs>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import AchExaminationModal from './modules/AchExaminationModal'
import { getAllGrade, getSchoolYearDataList, getSemesterDataList } from '@api/commonAPI'
import { getAchExaminationList } from '@api/achievementCommonAPI'
import { getAction, postAction } from '@api/manage'
import AchExaminationPaperListTables from '../achexaminationpaper/AchExaminationPaperListTables'
import AchAchieveStandardItemListTable from '../achachievestandarditem/StandardItemForPointsRules'
import AchAchieveStandardItemStandListTable from '../achachievestandarditem/StandardItemForGrading'

export default {
  name: 'AchExaminationList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    AchAchieveStandardItemStandListTable,
    AchExaminationModal,
    AchExaminationPaperListTables,
    AchAchieveStandardItemListTable
  },
  data() {
    return {
      copyExamVisible: false,
      examStartTime: '',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      type: 'radio',
      description: 'ach_examination管理页面',
      achExaminationList: [],
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '考试编码',
          align: 'center',
          dataIndex: 'examinationCode'
        },
        {
          title: '考试名称',
          align: 'center',
          dataIndex: 'examinationName'
        },
        {
          title: '考试类型',
          align: 'center',
          dataIndex: 'examinationTtype_dictText'
        },
        {
          title: '考试学年',
          align: 'center',
          dataIndex: 'schoolYearName'
        },
        {
          title: '考试学期',
          align: 'center',
          dataIndex: 'semesterName'
        },
        {
          title: '考试年级',
          align: 'center',
          dataIndex: 'gradeName'
        },
        {
          title: '是否启用',
          align: 'center',
          dataIndex: 'izOpen_dictText'
        },
        {
          title: '所属部门',
          align: 'center',
          dataIndex: 'sysOrgName'
        },
        {
          title: '创建人',
          align: 'center',
          dataIndex: 'createBy'
        },
        {
          title: '创建时间',
          align: 'center',
          sorter: true,
          dataIndex: 'createTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/achexamination/achExamination/getAchExaminationList',
        delete: '/achexamination/achExamination/delete',
        deleteBatch: '/achexamination/achExamination/deleteBatch',
        exportXlsUrl: '/achexamination/achExamination/exportXls',
        importExcelUrl: 'achexamination/achExamination/importExcel',
        copyExamUrl: '/achexamination/achExamination/copyExamAll'
      },
      dictOptions: {},
      superFieldList: [],
      searchParams: {
        id: null,
        examinationCode: null,
        examinationName: null,
        examinationTtype: null,
        gradeId: null,
        schoolYearId: null,
        semesterId: null,
        izOpen: null, //默认启用
        sysOrgCode: null,
        tenantId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        ignoreProperty: "semesterId"
      },
      queryParamSelect: {
        ignorePropertyList: [
          {label: '学年', value: 'schoolYearId'},
          {label: '学期', value: 'semesterId'},
          {label: '部门', value: 'sysOrgCode;subDepSysOrgCodes'},
          {label: '届级', value: 'studentEnrollmentYear'}
        ],
      },
      schoolYearDataList: [],
      semesterDataList: [],
      gradeDataList: [],
      itemSubjectList: [],
      oldExamInfo: {},
      newExamInfo: {},
    }
  },
  created() {
    this.getAchExamination()
    this.getSuperFieldList()
    this.getSchoolYearData()
    this.getSemesterData()
    this.getGradeData()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    clickThenCheck(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(','), [record])
          }
        }
      }
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
      if (this.selectedRowKeys.length >= 1) {
        this.$refs.AchExaminationPaperListTables.getOrderMain(this.selectedRowKeys[0])
        this.$refs.AchAchieveStandardItemListTable.getOrderMain(this.selectedRowKeys[0])
        this.$refs.AchAchieveStandardItemStandListTable.getOrderMain(this.selectedRowKeys[0])
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
    },
    initDictConfig() {
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'examinationCode', text: '考试编码', dictCode: '' })
      fieldList.push({ type: 'string', value: 'examinationName', text: '考试名称', dictCode: '' })
      fieldList.push({
        type: 'int',
        value: 'examinationTtype',
        text: '类型',
        dictCode: ''
      })
      fieldList.push({ type: 'string', value: 'gradeId', text: '年级', dictCode: '' })
      fieldList.push({ type: 'string', value: 'schoolYearId', text: '学年', dictCode: '' })
      fieldList.push({ type: 'string', value: 'semesterId', text: '学期', dictCode: '' })
      fieldList.push({ type: 'int', value: 'izOpen', text: '是否启用(1是启用，0否停用)', dictCode: '' })
      fieldList.push({ type: 'string', value: 'sysOrgCode', text: '所属部门编码', dictCode: '' })
      fieldList.push({ type: 'string', value: 'tenantId', text: '学校id', dictCode: '' })
      fieldList.push({ type: 'int', value: 'delFlag', text: '0表示未删除,1表示删除', dictCode: '' })
      fieldList.push({ type: 'string', value: 'createBy', text: '创建人', dictCode: '' })
      fieldList.push({ type: 'date', value: 'createTime', text: '创建时间' })
      fieldList.push({ type: 'string', value: 'updateBy', text: '修改人', dictCode: '' })
      fieldList.push({ type: 'date', value: 'updateTime', text: '修改时间' })
      this.superFieldList = fieldList
    },
    //条件查询
    whereSerchAchExamination() {
      this.getAchExamination()
    },
    //重置查询条件
    whereSerchAchExaminationInit() {
      this.searchParams = []
      this.getAchExamination()
    },
    async getAchExamination() {
      let achExamination = this.searchParams
      let res = await getAchExaminationList(achExamination)
      if (res.code === 200) {
        this.achExaminationList = res.result.records
      } else {
        this.$message.error('暂无数据或获取失败')
      }
    },
    async getSchoolYearData() {
      let res = await getSchoolYearDataList()
      this.schoolYearDataList = []
      res.result.forEach(item => {
        this.schoolYearDataList.push({
          label: item.schoolYearName,
          value: item.id
        })
      })
    },
    async getSemesterData() {
      let res = await getSemesterDataList()
      this.semesterDataList = []
      res.result.forEach(item => {
        this.semesterDataList.push({
          label: item.semesterName,
          value: item.id
        })
      })
    },
    async getGradeData() {
      let res = await getAllGrade()
      this.gradeDataList = []
      res.result.forEach(item => {
        this.gradeDataList.push({
          label: item.gradeName,
          value: item.id
        })
      })
    },
    copyExamAllThings() {
      this.achExaminationList.forEach(item => {
        if (item.id == this.selectedRowKeys) {
          this.newExamInfo = {}
          //深浅拷贝引发问题
          this.newExamInfo = JSON.parse(JSON.stringify(item))
        }
      })
      this.copyExamVisible = true
    },
    submitCopy() {
      let params = {
        // oldExamInfo: JSON.stringify(this.oldExamInfo),
        newExamInfo: JSON.stringify(this.newExamInfo),
        startTime: this.examStartTime
      }
      postAction(this.url.copyExamUrl, params).then(res => {
        if (res.success) {
          this.$message.success('复制成功')
          this.copyExamVisible = false
          this.loadData(1)
        } else {
          this.$message.error('复制失败')
        }
      })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>